package com.xsz.controller;

import com.xsz.entity.Administrator;
import com.xsz.entity.User;
import com.xsz.service.AdministratorService;
import com.xsz.util.MD5Utils;
import com.xsz.util.ResultVOUtil;
import com.xsz.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("administrator")
@Api(value = "管理员控制类")
public class AdministratorController {
    @Resource
    AdministratorService administratorService;

    @ApiOperation(value = "登录API")
    @PostMapping("/login")
    public ResultVO selectByNamePwd(@RequestBody Administrator administrator) {
        String password=administrator.getPwd();
        String administratorname=administrator.getName();
        password = MD5Utils.encrypt(administratorname, password);
        UsernamePasswordToken token = new UsernamePasswordToken(administratorname, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return ResultVOUtil.success();
        } catch (UnknownAccountException e) {
            return ResultVOUtil.error(500,e.getMessage());
        } catch (IncorrectCredentialsException e) {
            return ResultVOUtil.error(500,e.getMessage());
        } catch (LockedAccountException e) {
            return ResultVOUtil.error(500,e.getMessage());
        } catch (AuthenticationException e) {
            return ResultVOUtil.error(500,"登录失败");
        }
    }

    @ApiOperation(value = "注册API")
    @PostMapping("/register")
    public ResultVO add(@RequestBody Administrator administrator) {
        String password = MD5Utils.encrypt(administrator.getName(), administrator.getPwd());
        administrator.setPwd(password);
        return ResultVOUtil.success(administratorService.add(administrator));
    }

    @ApiOperation(value = "查询所有用户API")
    @GetMapping("/showAlluser")
    public ResultVO selectAlluser() {
        return ResultVOUtil.success(administratorService.getAllUser());
    }

    @ApiOperation(value = "根据用户账号查询用户API")
    @PostMapping("/showByUserName")
    public ResultVO selectByUserName(String name) {
        return ResultVOUtil.success(administratorService.getByUserName(name));
    }

    @ApiOperation(value = "根据用户账号关键字查询用户API")
    @PostMapping("/showByUserLikeName")
    public ResultVO selectByUserLikeName(String name) {
        return ResultVOUtil.success(administratorService.getByUserLikeName(name));
    }

    @ApiOperation(value = "重装用户密码API")
    @PostMapping("/updatepwd/{name}")
    public ResultVO updatepwd(@PathVariable("name") String name) {
        administratorService.updatepwd(name);
        return ResultVOUtil.success();
    }

    @ApiOperation(value = "查询申请vip的用户API")
    @GetMapping("/showapply")
    public ResultVO showapply() {
        return ResultVOUtil.success(administratorService.getByRole());
    }

    @ApiOperation(value = "批准vip用户申请API")
    @PostMapping("/updaterole/{id}")
    public ResultVO updaterole(@PathVariable("id") String id) {
        administratorService.updaterole(id);
        return ResultVOUtil.success();
    }



}
